package com.hfen;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @program: websocketnetty
 * @description: 初始化连接时候的各个组件
 * 继承时要泛型
 * @author: hfen
 * @create: 2021-08-12 10:02
 **/
public class MyWebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {

    /**
     * pipeline ： 流水线，完成一项工作，需要经过的工序
     * http-codec：HttpRequestDecoder 和HttpResponseEncoder的组合 编解码器
     * aggregator：HttpObjectAggregator主要是将HttpRequest和HttpContent合并成FullHttpRequest, 继承自MessageAggregator
     * ChunkedWriteHandler ： 将大数据拆分成小数据，用于支持异步写大数据流并且不需要消耗大量内存也不会导致内存溢出错误( OutOfMemoryError )
     * MyWebSocketHandler ： WebSocket业务处理类
     */
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        socketChannel.pipeline().addLast("http-codec", new HttpServerCodec());
        socketChannel.pipeline().addLast("aggregator", new HttpObjectAggregator(65535));
        socketChannel.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
        socketChannel.pipeline().addLast("handler", new MyWebSocketHandler());
    }
}
